package com.soap.desensitize.posterity.entity;

import com.soap.desensitize.encry.IDesensitization;
import com.soap.desensitize.exception.strategy.DecodeException;
import com.soap.desensitize.exception.strategy.EncodeException;
import com.soap.desensitize.posterity.AbstractPosterity;
import com.soap.desensitize.strategy.rreversible.symmetry.DesStrategy;

import java.lang.reflect.Field;

public class EntityDesensitization<T> extends AbstractPosterity<T> {

    /**
     * @desc 无参构造器，默认使用DES加密
     */
    private EntityDesensitization() {
        super(DesStrategy.getInstance());
    }

    private static EntityDesensitization instance = null;

    /**
     * @desc 获得实例
     * @return
     */
    public static synchronized EntityDesensitization getInstance(){
        if(instance == null){
            instance = new EntityDesensitization();
        }
        return instance;
    }

    /**
     * @desc 获得实例
     * @param encry 脱敏策略
     * @return
     */
    public static synchronized EntityDesensitization getInstance(IDesensitization<Object> encry){
        if(instance == null){
            instance = new EntityDesensitization(encry);
        }
        return instance;
    }
    /**
     * @param encry
     * @desc 有参构造器
     */
    private EntityDesensitization(IDesensitization encry) {
        super(encry);
    }


    @Override
    public T encode(T data) throws EncodeException {
        Class<?> clazz = data.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field:fields) {
            FieldDesensitize anno = field.getAnnotation(FieldDesensitize.class);
            if (anno!=null){
                field.setAccessible(true);
                try {
                    Object value = field.get(data);
                    Object encodeValeu = judgeAndEncode(value);
                    field.set(data,encodeValeu);
                    System.out.println();
                } catch (IllegalAccessException e) {
                    throw new EncodeException("属性值获取异常 "+data+" 错误信息"+e.getMessage());
                }
            }
        }
        return data;
    }

    @Override
    public T decode(T data) throws DecodeException {
        Class<?> clazz = data.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field:fields) {
            FieldDesensitize anno = field.getAnnotation(FieldDesensitize.class);
            if (anno!=null){
                field.setAccessible(true);
                try {
                    Object value = field.get(data);
                    Object decodeValue = judgeAndDecode(value);
                    field.set(data,decodeValue);
                    System.out.println();
                } catch (IllegalAccessException e) {
                    throw new DecodeException("属性值获取异常 "+data+" 错误信息"+e.getMessage());
                } catch (DecodeException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return data;
    }
}
